/*
 * This file is part of jVisualFS.
 * Version: release-0.8_beta
 *
 * jVisualFS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jVisualFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jVisualFS.  If not, see <http://www.gnu.org/licenses/>.
 */

package jvisualfs;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;


/**
 *
 * @author Dag Rune Sneeggen
 */
public class GenFX {
    // Field members...
    private ImagePanel ip;
    private BufferedImage img;
    private Graphics2D g2d;
    private DirCollection dirs;
    //private DirStruct dir;
    //private int height;
    private int imgWidth;
    private int imgHeight;
    
    // Constructor
    public GenFX(ImagePanel ip) {
        this.ip = jvisualfs.MainUI.VisPanel;
        this.imgWidth = 400;
        this.imgHeight = 420;
        this.img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
        this.g2d = img.createGraphics();
        this.dirs = new DirCollection();
        
    }
    public void addAllComps (DirCollection AllDirs, DirStruct Parent) {
        for (int j = 1; j < AllDirs.size(); j++) {
            
            DirStruct tmp = (DirStruct) AllDirs.getNum(j);
            //System.out.println("POS: " +  j + " NAME: " + tmp.getName() + " SIZE: " + tmp.getLength() + " PARENT: " + tmp.getParent().getName());
            //if (tmp.hasParent("c:/DeusEx")) {
            System.out.println(jvisualfs.MainUI.VisFX.dirs.size() + " P: " +  tmp.getName() + " . " + tmp.getParent().getName() + " ROOT: " +  Parent.getName());
            if (tmp.getParent().equals(Parent)) {
                //System.out.println("PARPOS: " +  j + " NAME: " + tmp.getName() + " SIZE: " + tmp.getLength() + " PARENT: " + tmp.getParent().getName());
                System.out.println(j + " POO: " +  tmp.getName() + " . " + tmp.getParent().getName() + " ROOT: " +  Parent.getName());
                jvisualfs.MainUI.VisFX.addComp(tmp);
            }
            
        }
        if (jvisualfs.MainUI.VisFX.dirs.size() == 0) {
            System.out.println("ZERO: ");
            jvisualfs.MainUI.VisFX.addComp(Parent);
        }
    }
    public void clear () {
        dirs.clear();
    }
    public void addComp (DirStruct dir) {
        dirs.add(dir);
    }
    public DirStruct getComp(int k) {
        return (DirStruct) this.dirs.getNum(k);            
    }
    public DirStruct getCoordFile(int x, int y) {
        DirStruct tmp = (DirStruct) this.dirs.getNum(0);
        for (int j = 0; j < this.dirs.size(); j++) {
            tmp = (DirStruct) this.dirs.getNum(j);
            int[] co = tmp.getCoord();
            //System.out.println(" X1 " + co[0] + " X2 " + co[1] + " Y1 " + co[2] + " Y2 " + co[3]);
            if ((x > co[0] && x < co[1]) && (y > co[2] && y < co[3])) {
                //System.out.println("YES::: X1 " + co[0] + " X2 " + co[1] + " Y1 " + co[2] + " Y2 " + co[3]);
                return tmp;
            }
        }
        return tmp;
    }
    public boolean isCoord(int x, int y) {        
        //System.out.println("X " + x + " Y " + y);
        for (int j = 0; j < this.dirs.size(); j++) {
            DirStruct tmp = (DirStruct) this.dirs.getNum(j);
            int[] co = tmp.getCoord();
            //System.out.println(" X1 " + co[0] + " X2 " + co[1] + " Y1 " + co[2] + " Y2 " + co[3]);
            if ((x > co[0] && x < co[1]) && (y > co[2] && y < co[3])) {
                //System.out.println("YES::: X1 " + co[0] + " X2 " + co[1] + " Y1 " + co[2] + " Y2 " + co[3]);
                return true;
            }
        }
        return false;
    }
    public void removeAllHiLight() {
        for (int l = 0; l < this.dirs.size(); l++) {
            DirStruct tmp = (DirStruct) this.dirs.getNum(l);
            tmp.setHilight(false);
        }
    }
    public void setHiLightByCoord(int x, int y) {
        for (int k = 0; k < this.dirs.size(); k++) {
            DirStruct tmp = (DirStruct) this.dirs.getNum(k);
            int[] co = tmp.getCoord();
            if ((x > co[0] && x < co[1]) && (y > co[2] && y < co[3])) {
                //System.out.println("HIGHLIGHT??????????" + tmp.getName());                
                tmp.setHilight(true);
            }
        }
    }
    public void setImage(ImagePanel ip) {
        this.ip = ip;
    }
    public void redrawBlank() {
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, imgWidth, imgHeight);
        ip.setImage(img);
        ip.repaint();
    }
    public void redraw() {
        //System.out.println("DIRLEN:" + dirs.getNum(0).getLength());
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, imgWidth, imgHeight);

        //int[] cols = { 480225, 435654 , 401236, 369878, 300654, 208765, 150879, 135888, 120245, 90000, 70342, 50987, 22222, 15000};
        float magicInt = ((imgHeight - 10) / (float) dirs.getNum(0).getLength());
        for (int j = 0; j < dirs.size(); j++) {
            int h = Math.round(dirs.getNum(j).getLength() * magicInt);
            if (h < 2) {
                h = h + 2;
            }
            float hue = ((0.071f * imgHeight)/h);
            //System.out.println("HH HH HH H H: " + h + " NUM: " + j + " HUE: " + hue);
            float br = 0.9f;
            if (dirs.getNum(j).isHilight()) {
                br = 1.0f;
            }
            g2d.setColor(Color.getHSBColor(hue,1,br));
            g2d.fill3DRect((10+(30*j)), (imgHeight-h), 20, h, true);
            int[] coord = {(10+(30*j)), (10+(30*j)+20), (imgHeight-h), (imgHeight)};
            dirs.getNum(j).setCoord(coord);
        }
        this.ip = jvisualfs.MainUI.VisPanel;
        //System.out.println("IMG: " + ip);
        ip.setImage(img);
        ip.repaint();
        //System.out.println("DIR: " + dirs.getNum(0).getParent().getName() + "ROOT" + jvisualfs.MainUI.DSRoot.getName());
        if (dirs.getNum(0).getParent().getName().equals(jvisualfs.MainUI.DSRoot.getName())) {
            jvisualfs.MainUI.btnBack.setEnabled(false);
        } else {
            jvisualfs.MainUI.btnBack.setEnabled(true);
        };
    }
}